www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/controllers/CommentController.php

    <?php
class CommentController extends CController
{
	public function actionList()
	{
	    //sleep(3);
	    $pid = $_POST['postid'];
		if (!app()->request->isAjaxRequest || !app()->request->isPostRequest) {
			echo '非法请求';
			exit;
		}
		
		$criteria = new CDbCriteria();
		// 评论显示的条数
        $criteria->limit = param('commentListNums');
        $criteria->order = 'id asc';
        $criteria->condition = 'post_id = :postId and isshow = ' . Comment::YES;
        $criteria->params = array('postId' => $pid);
        $comments = Comment::model()->findAll($criteria);
        $criteria->condition .= ' and support_nums >= :hotNums';
        // 成为热门评论需要的支持数
        $criteria->params['hotNums'] = param('hotCommentSupportNums'); 
        $hotComments = Comment::model()->findAll($criteria);
        
		$data['comments'] = $this->renderPartial('list', array(
			'comments' => $comments,
		    'comment_nums' => $_POST['comment_nums'],
		), true);
		$data['hotComments'] = $this->renderPartial('hot_list', array(
			'hotComments' => $hotComments,
			'comment_nums' => $_POST['comment_nums'],
		), true);
		echo json_encode($data);
	}
	
	
    public function actionAjaxOperate()
    {
        //sleep(3);
        $marks = array('support'=>Comment::OPERATE_SUPPORT, 'oppose'=>Comment::OPERATE_OPPOSE, 'neutral'=>Comment::OPERATE_NEUTRAL, 'report'=>Comment::OPERATE_REPORT);
		$fields = array_keys($marks);
        $field = trim($_GET['field']);
		$cid = (int)$_GET['cid'];
		
        if (!app()->request->isAjaxRequest || !in_array($field, $fields)) {
			echo '非法请求';
			exit;
		}
		/*
        if (!empty($_COOKIE['commentAjaxOperateExpire']) && !user()->checkAccess('editor')) {
		    $data['errno'] = 2;
		    $data['note'] =  sprintf('<div class="failed-note prompt" title="点击关闭">坐下喝杯茶吧,%d秒内只允许支持一次!</div>', param('commentOperateExpire'));
		    echo json_encode($data);
		    exit;
		}
		*/
		$cm['comment_id'] = $cid;
	    $cm['mark'] = $marks[$field];
		$cm['mark_user'] = user()->isGuest ? app()->session->sessionId : user()->getState('nickname');
	    $cm['mark_ip'] = CdcBetaTools::getClientIp();
	    $cmark = CommentMark::model()->findByAttributes($cm);
	    if (!empty($cmark)) {
	        $data['errno'] = 1;
		    $data['note'] =  '<div class="failed-note prompt" title="点击关闭">您已经投过票!</div>';
		    echo json_encode($data);
		    exit;
	    }
	    
		$comment = Comment::model()->findByPk($cid);
		$field_nums = $field . '_nums';
		$comment->$field_nums += 1;
		$result = $comment->update();
		
		if ($result) {
		    $mr = new CommentMark();
	        $mr->comment_id = $cm['comment_id'];
	        $mr->mark = $cm['mark'];
	        $mr->mark_user = $cm['mark_user'];
	        $mr->mark_ip = $cm['mark_ip'];
	        $mr->mark_time = $_SERVER['REQUEST_TIME'];
	        $mr->save();
	        // setcookie('commentAjaxOperateExpire', md5($cid), $_SERVER['REQUEST_TIME'] + 5, param('cookie_path'), param('cookie_domain'));
		    $data['errno'] = 0;
		    $data['note'] =  sprintf('<div class="success-note prompt" title="点击关闭">感谢您的%s!</div>', ($field == 'report') ? '举报' : '参与');
		    echo json_encode($data);
		    exit;
		} else {
		    $data['errno'] = -1;
		    $data['note'] = '<div class="failed-note prompt" title="点击关闭">系统错误</div>';
		    echo json_encode($data);
		    exit;
		}
    }
    
    public function actionAjaxManagerOperate()
    {
        //sleep(3);
        
        if (!user()->checkAccess('editor')) {
            $data['errno'] = -2;
		    $data['note'] = '<div class="failed-note prompt" title="点击关闭">您没有权限</div>';
		    echo json_encode($data);
		    exit;
        }
        
        $fields = array('recommend', 'isshow');
        $field = trim($_GET['field']);
		$cid = (int)$_GET['cid'];
		
        if (!app()->request->isAjaxRequest || !in_array($field, $fields)) {
			echo '非法请求';
			exit;
		}
		
		$comment = Comment::model()->findByPk($cid);
		$comment->$field = abs($comment->$field - 1);
		$result = $comment->update();
		
		if ($result) {
		    $data['errno'] = 0;
		    $note = ($field == 'recommend') ? 
		        ($comment->$field == Comment::YES) ? '此评论已经被推荐为精彩评论' : '此精彩评论已经被取消':
		        ($comment->$field == Comment::YES) ? '此评论已经被设置为显示状态' : '此评论已经被设置为隐藏状态';
		    $data['note'] =  sprintf('<div class="success-note prompt" title="点击关闭">%s!</div>', $note);
		    $data['label'] = ($field == 'recommend') ? 
		        ($comment->$field == Comment::YES) ? '取消推荐' : '推荐' :
		        ($comment->$field == Comment::YES) ? '隐藏' : '显示';
		    echo json_encode($data);
		    exit;
		} else {
		    $data['errno'] = -1;
		    $data['note'] = '<div class="failed-note prompt" title="点击关闭">系统错误</div>';
		    echo json_encode($data);
		    exit;
		}
    }
    
    public function actionAjaxCreate()
    {
        //sleep(3);
        if (!app()->request->isAjaxRequest || !app()->request->isPostRequest) {
			$data['errno'] = -1;
			$data['note'] =  '<div class="failed-note prompt" title="点击关闭">非法请求!</div>';
		    echo json_encode($data);
			exit(0);
		}
		
		if (!empty($_COOKIE['postCommentExpire'])) {
		    $data['errno'] = 2;
		    $data['note'] =  sprintf('<div class="failed-note prompt" title="点击关闭">%s秒内只允许评论一次!</div>', param('commentInterval'));
		    echo json_encode($data);
		    exit(0);
		}
		
        $postid = (int)trim($_POST['postid']);
        //$code = trim($_POST['validateCode']);
        $content = strip_tags($_POST['content']);
        $cid = $_GET['cid'];
        
        if (!empty($cid)) {
            $parent = Comment::model()->findByPk($cid);
            $nums = substr_count($parent->content, '<legend>');
            if ($nums > 10) {
                $data['errno'] = 3;
    		    $data['note'] =  '<div class="failed-note prompt" title="点击关闭">楼层最多只能盖10层,别偷懒了,从头盖吧!</div>';
    		    echo json_encode($data);
    		    exit(0);
            }
            $content = sprintf('<fieldset class="quote"><legend>引用%s<i>%s</i>的评论:</legend>%s</fieldset>', $parent->CommentUser, date('Y-m-d H:i:s', $parent->post_time), $parent->content) . $content;
        }
        $comment = new Comment();
        $comment->post_id = $postid;
        $comment->content = $content;
        $result = $comment->save();
        if ($result) {
            setcookie('postCommentExpire', md5($postid), $_SERVER['REQUEST_TIME'] + param('commentInterval'), param('cookie_path'), param('cookie_domain'));
            $data['errno'] = 0;
		    $data['note'] =  '<div class="success-note prompt" title="点击关闭">发表评论成功!</div>';
		    $data['html'] = $this->renderPartial('only_list', array(
				'comment' => $comment,
		    ), true);
		    echo json_encode($data);
        } else {
            $data['errno'] = 1;
		    $data['note'] =  '<div class="failed-note prompt" title="点击关闭">发表评论失败!</div>';
		    echo json_encode($data);
        }
    }
    
}